/*
	Das hier muss fuer jedes Device implementiert werden.
	Es muessen die Funktionen aus Controller.h implementiert sein.
*/

#include <stdio.h>
#include <irrlicht.h>
#include <Controller.h>
#include <wiiuse.h>

using namespace irr;

wiimote** wiimotes;
int max_wiimotes = 1;
static int x_res;
static int y_res;
static int cursor_x;
static int cursor_y;

bool camUp = true;
int counter = 20;
int min = 20;
int max = 85;

	void handle_ctrl_status(struct wiimote_t* wm) {
		printf("\n\n--- CONTROLLER STATUS [wiimote id %i] ---\n", wm->unid);

		printf("attachment:      %i\n", wm->exp.type);
		printf("speaker:         %i\n", WIIUSE_USING_SPEAKER(wm));
		printf("ir:              %i\n", WIIUSE_USING_IR(wm));
		printf("leds:            %i %i %i %i\n", WIIUSE_IS_LED_SET(wm, 1), WIIUSE_IS_LED_SET(wm, 2), WIIUSE_IS_LED_SET(wm, 3), WIIUSE_IS_LED_SET(wm, 4));
		printf("battery:         %f %%\n", wm->battery_level);
	}

	WiiController::WiiController(DeviceInfo *c){
		this->initController(c);
	}

	WiiController::WiiController(DeviceInfo *c, OptParams o, int length){
		memcpy(&x_res, (int*)o.info, 4);
		memcpy(&y_res, (int*)(o.info)+4, 4);
		cursor_x = x_res / 2;
		cursor_y = y_res / 2;
		this->initController(c);
	}

	WiiController::~WiiController(){
		wiiuse_cleanup(wiimotes, max_wiimotes);
	}

	void WiiController::initController(DeviceInfo *c) const{
		printf("Initialisiere Controller!\n");

		c->cursorXbin = false;
		c->cursorYbin = false;
		c->objectYBin = true;
		c->rotXbin = true;
		c->rotYbin = true;
		c->rotZbin = true;
		c->selectObjectBin = true;
		c->dropObjectBin = true;
		c->endApplicationBin = true;

		int found, connected;

		wiimotes = wiiuse_init(max_wiimotes);   //Feld initialisieren

		found = wiiuse_find(wiimotes, max_wiimotes, 5); //Wiimotes suchen (muessen dafuer schon verbunden sein!)
		if (!found) {
	        printf ("Keine Wiimotes gefunden.\n");
		    return;
		}

		connected = wiiuse_connect(wiimotes, max_wiimotes); //Wiimotes verbinden (mit der Anwendung)
		if (connected)
			printf("Verbunden zu %i Wiimotes (von %i gefundenen).\n", connected, found);
		else {
			printf("Konnte keine Verbindung zu einer Wiimote herstellen.\n");
			return;
		}	

		wiiuse_rumble(wiimotes[0], 1); //Hallo sagen
		Sleep(200);
		wiiuse_rumble(wiimotes[0], 0);

		wiiuse_set_ir(wiimotes[0], 1);
		wiiuse_set_ir_vres(wiimotes[0], x_res, y_res);

	    while(!wiiuse_poll(wiimotes, max_wiimotes));
		handle_ctrl_status(wiimotes[0]);

		printf("Controller ist da!\n");
	}

	void WiiController::updateController() const{
		wiiuse_poll(wiimotes, max_wiimotes);
	}

	void WiiController::drop() const{
		this->~WiiController();
	}

	void WiiController::getRotateX(ActionReturn *a) const{
	
	}

	void WiiController::getRotateY(ActionReturn *a) const{

	}

	void WiiController::getRotateZ(ActionReturn *a) const{

	}

	void WiiController::getRotateGame(ActionReturn *a) const{

	}

	void WiiController::getMoveCursorX(ActionReturn *a) const{
		int x = wiimotes[0]->ir.x;
		int delta = x - cursor_x;
		if(delta != 0){
			a->actionOccured = true;
			a->actionValue = (float)delta;
			cursor_x = x;
		}
	}

	void WiiController::getMoveCursorY(ActionReturn *a) const{
		int y = wiimotes[0]->ir.y;
		int delta = y - cursor_y;
		if(delta != 0){
			a->actionOccured = true;
			a->actionValue = (float)delta;
			cursor_y = y;
		}
	}

	void WiiController::getMoveObjectY(ActionReturn *a) const{

	}

	void WiiController::getSelectObject(ActionReturn *a) const{
		if(IS_JUST_PRESSED(wiimotes[0], WIIMOTE_BUTTON_A)){
			a->actionOccured = true;
		}
	}

	void WiiController::getDropObject(ActionReturn *a) const{
		if(IS_JUST_PRESSED(wiimotes[0], WIIMOTE_BUTTON_B)){
			a->actionOccured = true;
		}
	}

	void WiiController::getEndApplication(ActionReturn *a) const{

	}